﻿#region
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using ThuySan.Models;

#endregion

namespace ThuySan.Controllers
{
    public class ModuleController : Controller
    {
        private readonly UnitOfWork unitOfWork = new UnitOfWork();

        //
        // GET: /Module/

        public ActionResult Header()
        { 
            var logo =unitOfWork.SettingRepository.Select(c => c.Name.Equals("Logo")).FirstOrDefault();
            ViewBag.Logo = logo!=null?logo.Value:"";
            
            ViewBag.FoneNumber = unitOfWork.SettingRepository.Select(c => c.Name.Equals("FoneNumber")).FirstOrDefault().Value;
            return PartialView(unitOfWork.CategoryRepository.Select());
        }

        public ActionResult Footer()
        {
            var query = unitOfWork.TagRepository.Select(c => c.IsFooter == true)
                .Select(c => new { c.TagId, c.Name, c.PageUrl, c.IsStaticContent });
            List<dynamic> list = new List<dynamic>();
            foreach (var item in query)
            {
                dynamic obj = new DynamicViewDataDictionary();
                obj.TagId = item.TagId;
                obj.Name = item.Name;
                obj.PageUrl = item.PageUrl;
                obj.IsStaticContent = item.IsStaticContent;
                list.Add(obj);
            }
            return PartialView(list);
        }

        public ActionResult RightColumn()
        {
            var q1 = unitOfWork.TagRepository.Select(c => c.IsRightColumn ?? false && !(c.IsStaticContent??false));
            if (q1!=null)
            {
                var q2 = unitOfWork.ProductTagRepository.Select(c => c.TagId == q1.FirstOrDefault().TagId).OrderByDescending(c => c.Product.LastModified).Take(2);
                foreach (var tag in q1)
                {
                    if (q2 == null)
                    {
                        q2 = unitOfWork.ProductTagRepository.Select(c => c.TagId == tag.TagId).OrderByDescending(c => c.Product.LastModified).Take(2);
                    }
                    else
                    {
                        q2 = q2.Union(unitOfWork.ProductTagRepository.Select(c => c.TagId == tag.TagId).OrderByDescending(c => c.Product.LastModified).Take(2));
                    }
                }
                return PartialView(q2); 
            }
            
            //return PartialView(unitOfWork.ProductRepository.Select(c => c.IsReduced==true, "Category").ToList());
            
            return PartialView(null);
        }

        public ActionResult TopMenu()
        {
            return PartialView(unitOfWork.TagRepository.Select(c => c.IsTopMenu ?? false));
        }

        public ActionResult TopViewer()
        {
            return PartialView(unitOfWork.ProductRepository.Select(c => c.IsVisibled == true, c => c.Viewer, ListSortDirection.Descending).Skip(0).Take(10).ToList());
        }

        public ActionResult TagsList()
        {
            var query = unitOfWork.TagRepository.Select().SelectMany(c => c.ProductTags.DefaultIfEmpty(), (c, v) => new
            {
                Tag = new
                {
                    c.TagId,
                    c.Name,
                    c.PageUrl
                },
                count = v.Product.Viewer
            }).GroupBy(c => c.Tag).Select(c => new
            {
                c.Key.TagId,
                c.Key.Name,
                c.Key.PageUrl,
                Counter = c.Sum(v => v.count)
            });
            List<dynamic> list = new List<dynamic>();
            foreach (var item in query)
            {
                dynamic obj = new DynamicViewDataDictionary();
                obj.TagId = item.TagId;
                obj.Name = item.Name;
                obj.PageUrl = item.PageUrl;
                obj.Counter = item.Counter;
                list.Add(obj);
            }
            return PartialView(list);
        }

        public ActionResult SlideShow() //In Home page
        {
            var query = unitOfWork.ImageRepository.Select(c => c.IsBanner == true).OrderBy(c=>c.Priority??int.MaxValue).Select(c => new
            {
                CatUrl = c.Product.Category.PageUrl,
                ProdUrl = c.Product.PageUrl,
                ProdId = c.Product.ProductId,
                c.ImageName,
                c.Description
            });

            List<dynamic> list = new List<dynamic>();
            foreach (var item in query)
            {
                dynamic obj = new DynamicViewDataDictionary();
                obj.CatUrl = item.CatUrl;
                obj.ProdUrl = item.ProdUrl;
                obj.ProdId = item.ProdId;
                obj.ImageName = item.ImageName;
                obj.Description = item.Description;
                list.Add(obj);
            }
            return PartialView(list);
        }
    }
}
